//
//  ForgetPasswordViewController.m
//  Student
//
//  Created by 李飞 on 15/9/8.
//  Copyright (c) 2015年 XueYinYue. All rights reserved.
//

#import "ForgetPasswordViewController.h"
#import "ResetPasswordViewController.h"
#import <SMS_SDK/SMS_SDK.h>

@interface ForgetPasswordViewController ()
{
    int _timeout;
    dispatch_source_t _timer;
}
@end

@implementation ForgetPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"找回密码";
    //隐藏错误提示信息
    _errorSMGLab.hidden = YES;
    
    //调整textfield的输入框内输入文字距离
    UIView* userPaddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 90, _userNameTF.frame.size.height)];
    _userNameTF.leftView = userPaddingView;
    _userNameTF.leftViewMode = UITextFieldViewModeAlways;
    
    UIView* passPaddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 90, _identifyCodeTF.frame.size.height)];
    _identifyCodeTF.leftView = passPaddingView;
    _identifyCodeTF.leftViewMode = UITextFieldViewModeAlways;
    
    
    [self initNavegationBar];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/**
 *  创建改变获取验证码按钮状态的定时器
 */
- (void)createTimer
{
    _timeout = 60;
    dispatch_queue_t queue = dispatch_queue_create("timer queue", 0);
    _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(_timer, DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
    dispatch_source_set_event_handler(_timer, ^{
        if (_timeout > 0)
        {
            _timeout -= 1;
            dispatch_async(dispatch_get_main_queue(), ^{
                [_getVerificationCodeButton setTitle:[NSString stringWithFormat:@"获取验证码(%ds)",_timeout] forState:UIControlStateDisabled];
                [_getVerificationCodeButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateDisabled];
            });
        }
        else
        {
            dispatch_async(dispatch_get_main_queue(), ^{
                _getVerificationCodeButton.enabled = YES;
                [_getVerificationCodeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                [_getVerificationCodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
            });
            dispatch_source_cancel(_timer);
        }
    });
    dispatch_source_set_cancel_handler(_timer, ^{
        //        NSLog(@"timer cancle");
        _timer = nil;
    });
    dispatch_resume(_timer);
}

#pragma mark - NavegationBar

- (void)initNavegationBar
{
    
    //    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    //    backBtn.bounds = CGRectMake(0, 0, 15, 20);
    //    [backBtn addTarget:self action:@selector(onBackButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    //    [backBtn setBackgroundImage:[UIImage imageNamed:NAVIGATION_BACKARROW_IMAGE] forState:UIControlStateNormal];
    
    UIBarButtonItem *backBarBtn = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(onBackButtonClick:)];
    self.navigationItem.leftBarButtonItem = backBarBtn;
    
}

- (void)onBackButtonClick:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 错误提示信息
/**
 *  显示错误提示
 */
- (void)showErrorMessageTipsWithMessage:(NSString *)message
{
    //    [UIView transitionWithView:_errorSMGLab duration:0.3 options:(UIViewAnimationOptionTransitionFlipFromTop | UIViewAnimationOptionCurveEaseOut) animations:^{
    _errorSMGLab.hidden = NO;
    _errorSMGLab.text =message;
    //    } completion:nil];
    
}
/**
 *  显示错误提示
 */
- (void)hiddenErrorMessageTips
{
    //    [UIView transitionWithView:_errorSMGLab duration:0.3 options:(UIViewAnimationOptionTransitionFlipFromBottom | UIViewAnimationOptionCurveEaseOut) animations:^{
    _errorSMGLab.hidden = YES;
    //    } completion:nil];
    
}

#pragma mark - 手机号码检测
/**
 *  输入号码检查
 */
- (BOOL)checkNotPhoneNum
{
    if (_userNameTF.text.length != 11 ||  _userNameTF.text.length == 0) {
        return YES;
    }
    else{
        return NO;
    }
    
}
#pragma mark - 事件响应
- (IBAction)onIdentifyCodeClick:(id)sender {
    //手机号码输入检测
    if ([self checkNotPhoneNum])
    {
        //SHOW_ALERTVIEW(@"请正确输入手机号码", nil, 1, self);
        [self showErrorMessageTipsWithMessage:@"请正确输入手机号码"];
    }
    else
    {
        [self hiddenErrorMessageTips];
        
        _getVerificationCodeButton.enabled = NO;
        if (!_timer)
        {
            [self createTimer];
        }
        [SMS_SDK getVerificationCodeBySMSWithPhone:_userNameTF.text
                                              zone:@"86"
                                            result:^(SMS_SDKError *error) {
                                                NSLog(@"获取验证Error:%@",error);
                                            }];
    }
}

- (IBAction)onOkClick:(id)sender {
//    if ([self checkNotPhoneNum])
//    {
//        //SHOW_ALERTVIEW(@"请正确输入手机号码", nil, 1, self);
//        [self showErrorMessageTipsWithMessage:@"请正确输入手机号码"];
//    }
//    else
//    {
//       // 验证码核对
//        [SMS_SDK commitVerifyCode:_identifyCodeTF.text result:^(enum SMS_ResponseState state) {
//            if (state == 0)
//            {
//                // SHOW_ALERTVIEW(@"验证码错误", nil, 0, self);
//                
//                [self showErrorMessageTipsWithMessage:@"验证码错误"];
//            }
//            else
//            {
//                [self hiddenErrorMessageTips];
    
                ResetPasswordViewController *resetPasswordViewController = [[ResetPasswordViewController alloc] initWithNibName:@"ResetPasswordViewController" bundle:nil];
                [self.navigationController pushViewController:resetPasswordViewController animated:YES];

//            }
//        }];
//    }
}
@end
